package com.stephenone.zhdaily.comment.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.stephenone.zhdaily.R;
import com.stephenone.zhdaily.data.bean.Comment;
import com.stephenone.zhdaily.uitl.TimeUtil;

import de.hdodenhof.circleimageview.CircleImageView;
import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by Administrator on 2018/1/31.
 */

public class CommentViewBinder extends ItemViewBinder<Comment,CommentViewBinder.ViewHolder> {
    private Context mContext;
    public CommentViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_comments, parent,
                false);
        return new CommentViewBinder.ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Comment item) {
        Glide.with(mContext)
                .load(item.getAvatar())
                .into(holder.mAvatar);
        holder.mNickname.setText(item.getAuthor());
        holder.mContent.setText(item.getContent());
        if (item.getReply_to() != null) {
            //textview中指定某些字的样式
            holder.mReply.setText(Html.fromHtml("<font color='#000000'><b>//" + item.getReply_to().getAuthor() + "：</b></font>" + item.getReply_to().getContent()));
        }else {
            holder.mReply.setVisibility(View.GONE);
        }
        holder.mTime.setText(TimeUtil.timestampToFormattedTime(item.getTime() * 1000));
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public CircleImageView mAvatar;
        public TextView mNickname;
        public TextView mContent;
        public TextView mTime;
        public TextView mReply;
        public ViewHolder(View itemView) {
            super(itemView);
            mAvatar = itemView.findViewById(R.id.iv_avatar);
            mNickname = itemView.findViewById(R.id.tv_nickname);
            mContent = itemView.findViewById(R.id.tv_comment_content);
            mTime = itemView.findViewById(R.id.tv_time);
            mReply = itemView.findViewById(R.id.tv_reply);
        }
    }
}
